package models.repository;

import models.entities.OrderEntity;
import models.entities.OrderItem;
import models.entities.PaginatedList;
import models.entities.UserEntity;
import models.exceptions.CreateEntityException;
import models.exceptions.DeleteEntityException;
import models.exceptions.GetEntityException;
import models.helpers.DBSession;
import models.repository.common.OrderRepository;
import mybatis.mappers.OrderMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

@Deprecated
public class OrderRepositorySQL implements OrderRepository {
    public int place(int userId) throws CreateEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            OrderMapper orderMapper = dbSession.getMapper(OrderMapper.class);
            OrderEntity order = new OrderEntity(userId);
            orderMapper.placeOrder(order);
            dbSession.commit();
            return order.id;
        } catch (Exception ex) {
            throw new CreateEntityException(MessageFormat.format("Cannot place order userId: {0}; Original message: {1}", userId, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public OrderEntity getOrder(int orderId) throws GetEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            OrderMapper orderMapper = dbSession.getMapper(OrderMapper.class);
            List<OrderItem> orderItems = orderMapper.getOrderItems(orderId);
            UserEntity user = orderMapper.getUser(orderId);
            return new OrderEntity(orderId, orderItems, user);
        } catch (Exception ex) {
            throw new GetEntityException(MessageFormat.format("Cannot get order items: {0}; Original message: {1}", orderId, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public PaginatedList<OrderEntity> getOrderList(int pageNumber, int pageSize) throws GetEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            if (pageNumber == 0) {
                pageNumber = 1;
            }
            if (pageSize == 0) {
                pageSize = 20;
            }
            OrderMapper orderMapper = dbSession.getMapper(OrderMapper.class);
            List<OrderEntity> orders = orderMapper.getOrderList(pageNumber - 1, pageSize);
            int totalCount = orderMapper.getOrderListTotalCount();
            return new PaginatedList<OrderEntity>(orders, totalCount, pageSize);
        } catch (Exception ex) {
            throw new GetEntityException(MessageFormat.format("Cannot get order list; Original message: {0}", ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void delete(int id) throws DeleteEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            OrderMapper orderMapper = dbSession.getMapper(OrderMapper.class);
            orderMapper.deleteOrder(id);
            dbSession.commit();
        } catch (Exception ex) {
            throw new DeleteEntityException(MessageFormat.format("Cannot delete order; Original message: {0}", ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }
}
